package com.jinduplan.planmanager.ui.activity.user

import android.view.View
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.base.toCountDown
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.cxz.kotlin.baselibs.utils.UserInfoUtil
import com.fphoenixcorneae.util.toast.ToastUtil
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.LoginInfo
import com.jinduplan.planmanager.mvp.contract.LoginRegisterContract
import com.jinduplan.planmanager.mvp.presenter.LoginRegisterPresenter
import com.jinduplan.planmanager.ui.activity.MainActivity
import kotlinx.android.synthetic.main.activity_change_psw.*

class ChangePswAcitivity:
    BaseMvpTitleActivity<LoginRegisterContract.View, LoginRegisterContract.Presenter>(),LoginRegisterContract.View {

//    override fun initView() {
//        setStatusBarColor(resources.getColor(R.color.transparent_75))
//        setStatusBarIcon(false)
//        super.initView()
//        setBaseTitleColor(android.R.color.white)
//    }

    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_change_psw
    }


    override fun createPresenter(): LoginRegisterContract.Presenter {
        return LoginRegisterPresenter()
    }

    override fun start() {
        setBaseTitle("修改密码")
        btnChangePsw.setOnClickListener {
            val phone = etAccount.text.toString()
            val code = etCode.text.toString()
            val psw = etPsw.text.toString()
            if (phone.isEmpty()) {
                ToastUtil.show("请输入手机号")
                return@setOnClickListener
            }
            if (phone.isEmpty()) {
                ToastUtil.show("请输入验证码")
                return@setOnClickListener
            }
            if (phone.isEmpty()) {
                ToastUtil.show("请输入密码")
                return@setOnClickListener
            }
            mPresenter?.changePsw(phone,psw,code)
        }
        tvGetCode.setOnClickListener {
            val phone = etAccount.text.toString()
            if (!phone.isNullOrEmpty()) {
                val hashMap = HashMap<String, String>()
                hashMap.put("phone",phone)
                mPresenter?.smsSend(GsonHelper.getRequestBoby(hashMap))
            } else {
                ToastUtil.show("请输入手机号")
            }


        }
    }

    override fun getCodeSuccess() {
        tvGetCode.isEnabled = false
        60.toCountDown({
            tvGetCode.setText(it.toString()+"s")
        },{
            tvGetCode.isEnabled = true
            tvGetCode.setText("获取验证码")
        })
    }

    override fun getCodeFail() {
        tvGetCode.isEnabled = true
        tvGetCode.setText("获取验证码")
    }

    override fun loginSuccess(data: LoginInfo) {
    }

    override fun changePswSuccess(data: LoginInfo) {
        UserInfoUtil.logout(this)
    }

    override fun registerSuccess(data: LoginInfo) {
        UserInfoUtil.LoginSuccess(this,data.user_name,data.nick_name,data.score,data.position,data.avatar,data.labour,data.id)
    }

    override fun getSms(code: String) {
    }
}